using System;
using Microsoft.AspNetCore.Mvc;
using ApiProject.Api.Entity;
using System.Linq;
using ApiProject.Api.Repository;
using Microsoft.Extensions.Configuration;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;


// 文章类型管理
namespace ApiProject.Api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class ArticleTypeController : ControllerBase
    {
        private IRepository<ArticleTypes> _articleTypeRepository;
        private IRepository<Articles> _articlesRepository;

        private readonly IConfiguration _configuration;

        public ArticleTypeController(
            IConfiguration configuration,
            IRepository<ArticleTypes> articleTypeRepository,
            IRepository<Articles> articlesRepository
        )
        {
            _articleTypeRepository = articleTypeRepository;
            _articlesRepository = articlesRepository;
            _configuration = configuration;
        }


        //添加文章类型
        //Post :/articletype
        [HttpPost]
        public dynamic Post(NewArticle newArticle)
        {

            var type = _articleTypeRepository.Table.Where(x => x.ArticleType == newArticle.ArticleType).FirstOrDefault();

            if (type != null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "该文章类型已存在"
                });
            }
            else
            {

                var newType = new ArticleTypes
                {
                    ArticleType = newArticle.ArticleType
                };

                _articleTypeRepository.Insert(newType);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = newType,
                    Msg = string.Format("添加 {0} 成功", newArticle.ArticleType)
                });
            }

        }


        //通过文章类型Id获取获取所有文章
        [AllowAnonymous]
        [HttpPut, Route("artype/{id}")]
        public dynamic GetAllArticle(int id)
        {

            var allArticeById = _articlesRepository.Table.Where(x => x.ArticleTypeNum == id).Where(o=>o.IsEnable==true && o.IsDelete==false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = allArticeById,
                MissingFieldException = "成功"
            });
        }


        //获取所有文章类型
        // Get /articletype
        [AllowAnonymous]
        [HttpGet]
        public dynamic Get()
        {

            var allType = _articleTypeRepository.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = allType
            });
        }

        //禁用文章类型
        // Post /articletype/isenable/{id}
        // 参数 user的id
        [HttpPost, Route("isenable/{id}")]
        public dynamic IsEnable(int id, NewArticle model)
        {
            var type = _articleTypeRepository.GetById(id);

            var num = model.IsEnable;

            if (type != null)
            {
                if (num == false)
                {
                    type.IsEnable = false;
                    type.UpdatedTime = DateTime.Now;
                    _articleTypeRepository.Update(type);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = type,
                        Msg = string.Format("已禁用 {0} 的文章类型", type.ArticleType)
                    });
                }
                else
                {
                    type.IsEnable = true;
                    type.UpdatedTime = DateTime.Now;
                    _articleTypeRepository.Update(type);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = type,
                        Msg = string.Format("已启用 {0} 的文章类型", type.ArticleType)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "失败" });
            }
        }


        //修改文章类型名称
        //Put /articletype/{id}
        [HttpPut("{id}")]
        public dynamic EditType(int id, NewArticle model)
        {

            var type = _articleTypeRepository.GetById(id);

            type.ArticleType = model.ArticleType;

            _articleTypeRepository.Update(type);
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = type,
                Msg = "修改文章类型名称成功"
            });
        }

    }
}